package com.vpen.account.demo.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vpen.clib.constant.CConstant;
import com.vpen.clib.result.CResult;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
@AllArgsConstructor(onConstructor_ = @Autowired)
public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

    private final ObjectMapper objectMapper;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        //设置返回请求头
        response.setContentType(CConstant.CONTENT_TYPE);
        //写出流
        PrintWriter out = response.getWriter();
        out.write(objectMapper.writeValueAsString(CResult.success()));
        out.flush();
        out.close();
    }
}
